<html>
<head>
<title>C Data Types Not Supported By the Java Language</title>
</head>
<body>
<table width=100%>
<tr>
<td align=left>
<a href="index.html"><img src=../images/PreviousArrow.gif width=26 height=26 align=bottom border=0 alt="Previous | "></a><a
href="operators.html"><img src=../images/NextArrow.gif width=26 height=26 align=bottom border=0 alt="Next | "></a><a
href="../index.html"><img src=../images/WayUpArrow.gif width=26 height=26 align=bottom border=0 alt="Trail Map | "></a><a
href="./index.html"><img src=../images/noCHeader.gif width=26 height=26 align=bottom border=0 alt="How the Java Language Differs From C and C++ | "></a>
<td>
<td align=right>
<a href="index.html"><strong><em>How the Java Language Differs from C and C++</em></strong></a>
</td>
</tr>
</table>
<p>
<hr size=4>

<h2>
    C Data Types Not Supported By the Java Language
</h2>
<p>
<blockquote>

The Java language does not support pointers, <code>struct</code>, or <code>union</code>.

<h4>pointers</h4>
<blockquote>
The Java language passes all arrays and objects by reference but does not have
an explicit pointer type. You cannot construct a reference to anonymous
memory. In addition to making programming easier, this prevents
common errors due to pointer mismanagement.
</blockquote>

<h4>struct and union</h4>
<blockquote>
The Java language does not support either <code>struct</code> or <code>union</code>.
Instead use classes or interfaces to build composite types.
For example, in C, you might declare a structure that contains
information about employees like this:
<blockquote>
<pre>
struct employee {
    char name[NAMESIZE];
    char address[ADRSIZE];
    long ssn;
    double salary;
    double (*compute_raise)(double, double);
};
</pre>
</blockquote>
The last line of this structure is a pointer to a function.
When you allocate and initialize an employee structure,
you must supply the structure with a pointer to a function,
and that function must be defined as indicated in the struct
declaration. In the example above, the <code>compute_raise</code>
function must accept two doubles are arguments, and return a double.
This function will do the trick.
<blockquote>
<pre>
double compute_raise(double salary, double percent)
{
    return salary * percent;
}
</pre>
</blockquote>
Note that even though <code>salary</code> is part of the structure
we still need to supply that value to the function.
Here's a main program that creates, initializes an employee
structure, and then computes a 10% raise for that person.
Note that the main program has access to information (the employee's
salary) that ideally should be kept private.
<blockquote>
<pre>
main()
{
    struct employee George = {
        "George",
        "NOWHERE",
        123456789,
        45000.00,
        compute_raise
    };
    printf("raise = %f\n", George.compute_raise(George.salary, 0.10));
}
</pre>
</blockquote>
<p>
In the Java language, the need for structures is completely obsoleted by classes.
which provide for a cleaner way to bundle data and
methods together, and a way to keep some of that data private to the class.
<p>
In the Java language, instead of the struct declared above, you would declare a class to
maintain information about employees:
<blockquote>
<pre>
class Employee {
    String name;
    String address;
    long ssn;
    private double salary;
    double compute_raise(double percent) {
        return percent * salary;
    }
    Employee(String a_name, String a_address, long a_ssn, double a_salary){
        name = a_name;
        address = a_address;
        ssn = a_ssn;
        salary = a_salary;
    };
}
</pre>
</blockquote>
Note that a complete method declaration for <code>compute_raise</code>
including the method body is implemented within the class.
Also note that the salary instance variable is declared private--this
means that only an instance of this class has access to the salary
information-- thereby keeping the information protected from prying eyes.
Try doing that with C.
A constructor is included for convenience.
<p>
Notice how this application can compute George's raise without
ever obtaining the salary information directly.
<blockquote>
<pre>
class MainClass {
    public static void main(String args[]) {
        Employee george = new Employee("George", "NOWHERE", 123456789, 45000.0);
        System.out.println("raise = " + george.compute_raise(0.10));
    }
}
</pre>
</blockquote>
</blockquote>

</blockquote>
<p>
<hr size=4>
<p>
<table width=100%>
<tr>
<td align=left>
<a href="index.html"><img src=../images/PreviousArrow.gif width=26 height=26 align=top border=0 alt="Previous | "></a><a
href="operators.html"><img src=../images/NextArrow.gif width=26 height=26 align=top border=0 alt="Next | "></a><a
href="../index.html"><img src=../images/WayUpArrow.gif width=26 height=26 align=top border=0 alt="Trail Map | "></a><a
href="./index.html"><img src=../images/noCHeader.gif width=26 height=26 align=top border=0 alt="How the Java Language Differs From C and C++ | "></a>
<td>
<td align=right>
<a href="index.html"><strong><em>How the Java Language Differs from C and C++</em></strong></a>
</td>
</tr>
</table>
</body>
</html>
